package com.jin.spirng_boot_learn.jdbc;

//import org.apache.commons.dbcp.BasicDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class connet {

    public static final String URL = "jdbc:mysql://127.0.0.1:3306/spring_boot_learn";
    public static final String USER = "root";
    public static final String PASSWORD = "root";


//    public static void main(String[] args) throws Exception {
//        new connet().dbcpDemo();
//
//    }

//    public static boolean connectionMySqlDemo() {
//        Connection conn = null;
//        try {
//            // 1、动态加载mysql驱动
//            Class.forName("com.mysql.jdbc.Driver");
//            // 2、连接数据库
//            conn = DriverManager.getConnection(URL, USER, PASSWORD);
//            // 3、声明一个Statement 用来执行sql语句
//            Statement stmt = conn.createStatement();
//
//            // 4、执行sql语句
//            stmt.executeUpdate("create table student2(no_id char(20),name varchar(20),primary key(no_id))");
//
//            int result = stmt.executeUpdate("insert into student2(no_id,name) values('1','fxleyu')");
//            if (result > 0) {
//                ResultSet rs = stmt.executeQuery("select * from student2");
//                while (rs.next()) {
//                    System.out.println(rs.getString(1));
//                }
//            }
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            // 5、关闭数据库
//            if (conn != null) {
//                try {
//                    conn.close();
//                    return true;
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//        return false;
//    }
//
//    public void dbcpDemo() throws Exception {
//
//        BasicDataSource basicDataSource = new BasicDataSource();
//        basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        basicDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/spring_boot_learn");
//        basicDataSource.setUsername("root");
//        basicDataSource.setPassword("root");
//
//        basicDataSource.setInitialSize(3);
//        basicDataSource.setMaxActive(3);
//
//        basicDataSource.setMaxIdle(3); //待命 空闲链接
//        basicDataSource.setMinIdle(3);
//
//        basicDataSource.setMaxWait(1000);
//
//        Connection connection1 = basicDataSource.getConnection();
//
//
//        Statement statement = connection1.createStatement();
//        statement.executeQuery("lock table spring_boot_learn.student read;");
//        try {
//            connection1.close(); // 还给连接池
//            normalQuery(basicDataSource, "select * from spring_boot_learn.class;");
//            normalQuery(basicDataSource, "select * from spring_boot_learn.class;");
//            normalQuery(basicDataSource, "select * from spring_boot_learn.class;");
//            normalQuery(basicDataSource, "select * from spring_boot_learn.class;");
//        } catch (SQLException e) {
//            System.out.println(e);
//            normalQuery(basicDataSource, "select * from spring_boot_learn.class;");
//            System.out.println("---------------------------------");
//        }
//
//        System.out.println("---------------------------------");
//    }
//
//
//    private void normalQuery(BasicDataSource basicDataSource,String sql ) throws SQLException {
//        Connection connection = basicDataSource.getConnection();
//        Statement statement = connection.createStatement();
//        statement.executeQuery(sql);
//        connection.close();
//    }


}
